<?php
class Admin_IndexController extends  Zend_Controller_Action{
	var $admin;
	var $param;
	public function init(){
        $this->view->headTitle("Zegome - Admin");
        $this->param = $this->_request->getParams();
        $a = Zend_Auth::getInstance();
        $this->admin = $a->getIdentity();
        if($this->admin==NULL && $this->param['action']!="login" && $this->param['action']!="logout"){
        	$this->_redirect("admin/index/login");
        }
        else{
        	$this->view->admin = $this->admin;
        }
        //Zend_Debug::dump($_COOKIE);
        //Zend_Debug::dump($_SESSION);
    }

    public function indexAction(){
	
    }
    public function userAction(){
    	if($this->admin)
    	echo $this->admin->{"username"};
    	else echo "false";
    	exit();
    }
    public function loginAction(){
    	
    	if($this->admin)
    	$this->_redirect("admin");
    	if($this->_request->isPost()){
    		//check validate error tu form
    		//Zend_Debug::dump($this->_request->getParams());
    		$a = Zend_Auth::getInstance();
    		$auth = new Zgome_Admin();
    		if($auth->login($this->_request->getParams()) == true){
    			$this->_redirect("admin");
    			echo "true";
    		}
    		else echo "false";
    	}
    	//exit();
    }
    public function logoutAction(){
    	//parent::__logout();
    	$auth = new Zgome_Admin();
    	$auth->logout();
    	$this->_redirect("admin/index/login");
    	exit;
    }
    public function addgameAction() {
    	$request = $this->getRequest();
    	if($request->isPost()){
    		Zend_Debug::dump($request->getPost());
    		Zend_Debug::dump($request->getFile());
    	}
    }
    //add event
    public function eventAction(){
    	if($this->_request->isPost()){
    		$data = $this->_request->getParams();
    		unset($data["action"]);
    		unset($data["controller"]);
    		unset($data["module"]);
    		$data["create_admin"] = $this->admin->{"id"};
    		$event_modle = new Model_Event();
    		$event_modle->saveEvent($data);
    		echo "<h2>Created !!!</h2>";
    	}
    }
    //add event
    public function noticeAction(){
    	if($this->_request->isPost()){
    		$data = $this->_request->getParams();
    		unset($data["action"]);
    		unset($data["controller"]);
    		unset($data["module"]);
    		$data["sender"] = $this->admin->{"username"};
    		$data["user_id"] = -1;
    		$data["type"] =1;
    		$data["message"] = "1|".$data["message"];
    		$notice_modle = new Model_Message();
    		$notice_modle->save($data);
    		echo "<h2>Created !!!</h2>";
    	}
    }
    public function couponAction(){
    	if($this->_request->isPost()){
    		$model = new Model_Coupon();
    		$data = $this->_request->getParams();
    		unset($data["action"]);
    		unset($data["controller"]);
    		unset($data["module"]);
    		$data["create_user"] = $this->admin->{"username"};
    		$number = $data["number"];
    		unset($data["number"]);
    		for ($i = 0; $i < $number; $i++){
    			do{
    				$coupon = $this->__generateRandomString(13);
    			}
    			while (!$model->validateCoupon($coupon));
    			$data["value"] = $coupon;
    			$model->save($data);
    		}
    		echo "<h2>Created !!!</h2>";
    	}
    }
    public function giftAction(){
    	if($this->_request->isPost()){
    		$data = $this->_request->getParams();
    		unset($data["action"]);
    		unset($data["controller"]);
    		unset($data["module"]);
    		if(trim($data["coupon"]) =="")
    			unset($data["coupon"]);
    		$data["user"] = $this->admin->{"username"};
    		$modelUser = new Model_User();
    		$allUser = $modelUser->listAllId();
    		$modelGift = new Model_Gift();
    		foreach ($allUser as $u){
    			$data["user_recieve_id"] = $u["user_id"];
    			$modelGift->save($data);
    		}
    	}
    }
    public function __generateRandomString($length = 13) {
    	return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
    }
}
?>